package com.aliyun.messagemgt.web.controller;

import com.alibaba.algo.cooperation.feign.vo.res.UserEmployeeResVO;
import com.aliyun.messagemgt.common.dto.foura.FourAMenu;
import com.aliyun.messagemgt.common.dto.foura.FouraGetOrgListByParentIdQry;
import com.aliyun.messagemgt.common.dto.foura.FouraGetUserByOrgQry;
import com.aliyun.messagemgt.common.dto.foura.FouraOrgInfoDTO;
import com.aliyun.messagemgt.common.dto.foura.FouraOrgUserInfoDTO;
import com.aliyun.messagemgt.common.dto.foura.FouraPageRoleQry;
import com.aliyun.messagemgt.common.dto.foura.FouraPositionDTO;
import com.aliyun.messagemgt.common.dto.foura.FouraRoleDTO;
import com.aliyun.messagemgt.common.dto.foura.OrgInfo;
import com.aliyun.messagemgt.common.dto.response.ResultMap;
import com.aliyun.messagemgt.domain.repository.FouraRepository;
import com.aliyun.messagemgt.entities.foura.FourAUserResponse;
import com.aliyun.messagemgt.fouraplugin.FouraService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author twf
 */
@Api(tags = "4A相关接口")
@RestController
@RequestMapping(value = "/4A")
@Slf4j
@ConditionalOnProperty(prefix = "xxl.sso", name = "enable", havingValue = "false")
public class FouraFireeyeController {

    @Autowired
    private FouraService fouraService;
    @Autowired
    private FouraRepository fouraRepository;

    private static final String USER_ID="30001";
    private static final String USER_CODE="admin";
    private static final String USER_NAME="admin";
    private static final String EMPLOYEE_CODE="9000001";

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public String getUserInfo(HttpServletRequest request) {
        return USER_CODE;
    }

    /**
     * 获取用户详细信息
     */
    @RequestMapping(value = "/getUserAndExtUserByCode", method = RequestMethod.GET)
    public ResultMap getUserAndExtUserByCode(HttpServletRequest request, HttpServletResponse response) {
        FourAUserResponse fourAUserResponse = new FourAUserResponse();
        fourAUserResponse.setUserCode(EMPLOYEE_CODE);
        fourAUserResponse.setUserName(USER_NAME);
        fourAUserResponse.setUserId(USER_ID);
        return ResultMap.success(fourAUserResponse);
    }

    /**
     * 根据UserCode获取权限列表，返回前端
     */
    @RequestMapping(value = "/getAuthByUser", method = RequestMethod.GET)
    public ResultMap getAuthByUser() {
        FourAMenu fourAMenu = fouraService.getAuthByUser();
        return ResultMap.success(fourAMenu);
    }


    /**
     * 通过机构编码获取下级机构数据
     *
     * @param orgCode
     * @return
     */
    @RequestMapping(value = "/getOrgsByParentCode", method = RequestMethod.GET)
    public ResultMap getOrgsByParentCode(String orgCode) {
        return ResultMap.success(orgCode);
    }

    @GetMapping("/getAllDepartment")
    public ResultMap getAllDepartment() {
        return new ResultMap(fouraService.getAllDepartment());
    }

    /**
     * 通过机构编码(用户名)获取机构下人员数据
     *
     * @param orgCode
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/authority/orgUser")
    public ResultMap getUserByOrgCode(
            @RequestParam(value = "orgCode", required = false) String orgCode,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum) {
        return ResultMap.success(fouraService.getUserByOrgCode(orgCode, userName, pageSize, pageNum));
    }

    /* *
     * 获取机构根节点
     *
     * @return
     * */

    @RequestMapping(value = "/getRootOrg", method = RequestMethod.GET)
    public ResultMap getRootOrg() {
        return ResultMap.success(fouraService.getRootTree());
    }

    /**
     * 获取所有机构信息列表
     *
     * @return
     */
    @RequestMapping(value = "/getRootTree", method = RequestMethod.GET)
    public ResultMap getRootTree() {
        List<OrgInfo> orgBeans = fouraService.getRootTree();
        ResultMap<Object> resultMap = new ResultMap<>();
        resultMap.setSuccess(true);
        resultMap.setErrCode(ResultMap.SUCCESS);
        resultMap.setData(orgBeans);
        return resultMap;
    }

    /**
     * 获取用户详细信息
     */
    @RequestMapping(value = "/sync/4AData", method = RequestMethod.GET)
    public ResultMap orgOrUserInfoSynchronization() {
        Long start = System.currentTimeMillis();
        int count = fouraService.sync4AData();
        Long end = System.currentTimeMillis();
        System.out.println("================================同步数据用时ms：>" + (end - start));
        return ResultMap.success(count);
    }

    @ApiOperation(value = "根据组织Id查询用户")
    @RequestMapping(value = "/selectUserByOrg", method = RequestMethod.GET)
    @ApiResponses({
            @ApiResponse(code = 200, message = "组织用户信息", response = FouraOrgUserInfoDTO.class)
    })
    public ResultMap<List<FouraOrgUserInfoDTO>> selectUserByOrg(FouraGetUserByOrgQry qry) {
        List<FouraOrgUserInfoDTO> fouraOrgUserInfoDTOList = fouraService.selectUserByOrg(qry);
        return ResultMap.success(fouraOrgUserInfoDTOList);
    }

    /**
     * 查询子级组织
     */
    @ApiOperation(value = "查询子级组织", notes = "不传父级id，默认查根组织")
    @RequestMapping(value = "/orgs", method = RequestMethod.GET)
    @ApiResponses({
            @ApiResponse(code = 200, message = "组织信息", response = FouraOrgInfoDTO.class)
    })
    public ResultMap getOrgListByParentOrgId(@ApiParam("父级组织id，不传父级id，默认查根组织") @RequestParam(value = "parentOrgId", required = false) Long parentOrgId) {
        if (ObjectUtils.isEmpty(parentOrgId)) {
            parentOrgId = 0L;
        }
        FouraGetOrgListByParentIdQry qry = FouraGetOrgListByParentIdQry.builder()
                .parentOrgId(parentOrgId).build();
        List<FouraOrgInfoDTO> orgDtos = fouraService.getOrgListByParentOrgId(qry);
        return ResultMap.success(orgDtos);
    }

    /**
     * 分页查询
     */
    @ApiOperation("分页查询角色")
    @RequestMapping(value = "/selectRoleByPage", method = RequestMethod.GET)
    @ApiResponses({
            @ApiResponse(code = 200, message = "组织信息", response = FouraRoleDTO.class)
    })
    public ResultMap<PageInfo<FouraRoleDTO>> selectRoleByPage(FouraPageRoleQry qry) {
        PageInfo<FouraRoleDTO> pageInfo = fouraService.selectRoleByPage(qry);
        return ResultMap.success(pageInfo);
    }

    /**
     * 根据组织获取岗位信息
     */
    @ApiOperation(value = "根据组织获取岗位信息")
    @RequestMapping(value = "/positions/{orgId}", method = RequestMethod.GET)
    @ApiResponses({
            @ApiResponse(code = 200, message = "岗位信息", response = FouraPositionDTO.class)
    })
    public ResultMap selectRoleByPage(@ApiParam("组织id") @PathVariable("orgId") Long orgId) {
        List<FouraPositionDTO> res = fouraService.selectPositionByOrg(orgId);
        return ResultMap.success(res);
    }
}
